window.loginAction = {
    init: function() {
        $(".login-screen").css('min-height', $(window).height());
        loginAction.loginSubmit();
        if (navigator.splashscreen) navigator.splashscreen.hide();
    },
    loginSubmit: function() {
        var loginForm = $("#loginForm");
        loginForm.on('submit', function(e) {
            e.preventDefault();
            var getForm = $(this).serializeArray(),
                formData = {};
            for (var i = 0; i < getForm.length; i++) {
                formData[getForm[i].name] = getForm[i].value;
            }
            // console.log(formData);
            var loginName = formData.loginName,
                loginPsw = formData.loginPsw;

            if ($.trim(loginName) === '') {
                F7.addNotification({
                    title: '发货端登录',
                    message: '请输入用户名！'
                });
                setTimeout(function() {
                    F7.closeNotification(".notifications");
                    $("#loginName").focus();
                }, 2000);
                return false;
            }

            if (loginPsw === '') {
                $("#loginPsw").focus();
                F7.addNotification({
                    title: '发货端登录',
                    message: '请输入登录密码！'
                });
                setTimeout(function() {
                    F7.closeNotification(".notifications");
                    $("#loginPsw").focus();
                }, 2000);
                return false;
            }

            // 打印极光推送 注册ID
            console.log('~~~~~~~~~~~~~~~~~~~~~~~~' + window.RegistrationID);
            // appXhr.GET('/login/shipLogin', {
            //         loginName: loginName,
            //         loginPwd: loginPsw,
            //         // pushToken: window.RegistrationID
            //         pushToken: '100d855909464401be4'
            //     })
            //     .done(function(data) {
            //         // if (data.code == "100") {
            //         // window.plugins.jPushPlugin.resumePush();
            //         loginAction.saveLogin(data.content);
            //         // window.plugins.jPushPlugin.setAlias(loginName);

            //         loginAction.hide();

            //         /* } else if (data.code == "-1001") {
            //              F7.addNotification({
            //                  title: '发货端登录',
            //                  message: data.message
            //              });
            //              setTimeout(function() {
            //                  F7.closeNotification(".notifications");
            //                  $("#loginPsw").focus();
            //              }, 2000);
            //          }*/
            //         console.log(data);
            //     });


            // 测试用
            loginAction.saveLogin({
                loginName: "fh",
                loginPwd: "12345678"
            });
            loginAction.hide();

            return false;
        });
    },
    saveLogin: function(userInfo) {
        var timestamp = new Date().getTime();
        localStorage.setItem("logintime", timestamp);
        // localStorage.setItem("loginName", userInfo.loginName);
        // localStorage.setItem("loginPwd", userInfo.loginPwd);
        localStorage.setItem("loginName", 'fh');
        localStorage.setItem("loginPwd", '25D55AD283AA400AF464C76D713C07AD');
        localStorage.setItem("userInfo", JSON.stringify(userInfo));

    },
    clearLogin: function() {
        /*localStorage.removeItem('logintime');
        localStorage.removeItem("loginName");
        localStorage.removeItem("loginPwd");
        localStorage.removeItem("userInfo");*/
    },
    checkLogin: function() {
        var timestamp = localStorage.getItem("logintime"),
            nowTime = new Date().getTime(),
            timeDiff = nowTime - timestamp,
            isLogin = false;
        if (timeDiff > 604800000) {
            this.logout(1);
            isLogin = false;
        } else {
            var loginPwd = localStorage.getItem('loginPwd');
            if (loginPwd) {
                this.hide();
                isLogin = true;
            } else {
                this.logout(1);
                isLogin = false;
            }
        }

        return isLogin;
    },
    logout: function(status) {
        var loginNameInput = $("#loginName").find('input'),
            loginPswInput = $("#loginPsw").find('input');
        if (status) {
            loginNameInput.focus();
        } else {
            loginPswInput.val('').focus();
        }
        loginAction.clearLogin();
        loginAction.show();
        // window.plugins.jPushPlugin.setAlias('');
    },
    show: function() {
        var loginView = $("#loginView");
        $("#loginFormBox").addClass('on');
    },
    hide: function() {
        var loginView = $("#loginView");
        $("#loginFormBox").removeClass('on');
        F7.closeModal('.login-screen');
    },
    loginParams: function() {
        var loginName = localStorage.getItem("loginName"),
            loginPwd = localStorage.getItem("loginPwd"),
            loginParams = {
                loginName: loginName,
                loginPwd: loginPwd
            };

        return loginParams;
    }
};

module.exports = loginAction;
